<template>
	<view class="main">
		<view class="mains">
			<u-sticky>
				<view class="top_c">
					<view class="top">
						<view class="items">
							<view class="lefts">
								<text>代理</text>
							</view>
							<view class="rights">
								<text>{{agentData.name}}</text>
								<text class="phone">{{agentData.phone}}</text>
							</view>
						</view>
					</view>
				</view>
			</u-sticky>
			<view class="content">
				<view class="types">
					<view v-if="toTradesForm && toTradesForm.length > 0" class="lists">
						<view class="title">
							<text>分润</text>
						</view>
						<view class="items">
							<view class="lefts">
								<text>结算费率</text>
							</view>
							<view class="rights">
								<input type="text" :placeholder="'0 ~ ' + toTradesForm[0].max_money" v-model="toTradesForm[0].money">
								<text>‱</text>
							</view>
						</view>
					</view>
					<view v-if="toActivesForm && toActivesForm.length > 0" class="lists">
						<view class="title">
							<text>激活</text>
						</view>
						<view class="items">
							<view class="lefts">
								<text>返现金额</text>
							</view>
							<view class="rights">
								<input type="text" :placeholder="'0 ~ ' + toActivesForm[0].max_money" v-model="toActivesForm[0].money">
								<text>元</text>
							</view>
						</view>
					</view>
					<view v-if="toSimcardsForm && toSimcardsForm.length > 0" class="lists">
						<view class="title">
							<text>流量卡</text>
						</view>
						<view class="items">
							<view class="lefts">
								<text>返现金额</text>
							</view>
							<view class="rights">
								<input type="text" :placeholder="'0 ~ ' + toSimcardsForm[0].max_money" v-model="toSimcardsForm[0].money">
								<text>元</text>
							</view>
						</view>
					</view>
					<view v-if="toStandardsForm && toStandardsForm.length > 0" class="lists standard">
						<view class="title">
							<text>达标奖励</text>
						</view>
						<view class="list-item" v-for="(item,index) in toStandardsForm" :key="index">
							<view class="title">
								<text>奖励{{index + 1}}</text>
							</view>
							<view class="dayFrame">
								<view class="lefts">
									<input type="text" placeholder="开始天数" v-model="item.start_day" disabled="true">
								</view>
								<view class="centers">
									<text>~</text>
								</view>
								<view class="rights">
									<input type="text" placeholder="结束天数" v-model="item.end_day" disabled="true">
								</view>
							</view>
							<view class="items">
								<view class="lefts">
									交易金额
								</view>
								<view class="rights">
									<input type="text" placeholder="输入交易金额" v-model="item.trade_money" disabled="true">
									<text>元</text>
								</view>
							</view>
							<view class="items">
								<view class="lefts">
									奖励金额
								</view>
								<view class="rights">
									<input type="text" :placeholder="'0 ~ ' + item.max_money" v-model="item.money">
									<text>元</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="saveBtn" @click="saveData">
				<text>提交</text>
			</view>
		</view>
	</view>
</template>

<script>
	import tabbarItem from '../../../uni_modules/uview-ui/libs/config/props/tabbarItem'

	export default {
		data() {
			return {
				agent_id: 0,
				agentData: {},
				myTradesForm: null,
				myActivesForm: null,
				mySimcardsForm: null,
				myStandardsForm: null,
				toTradesForm: null,
				toActivesForm: null,
				toSimcardsForm: null,
				toStandardsForm: null,
				agentPolicy: null,
				settings: 1,
				is_true: true,
				title: '',
				data: [],
				policyMsg: '确定要提交吗？',
			}
		},
		onLoad(e) {
			this.agent_id = e.id
			this.getAgent()
			this.loadData()
		},
		methods: {
			// 获取代理
			async getAgent() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'user/getAgent',
					data: {
						agent_id: this.agent_id
					}
				});
				if (res.code == 200) {
					this.agentData = res.data
				}
			},
			// 获取数据
			async loadData() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getAgentHzyPolicy',
					data: {
						product_id: this.product_id,
						agent_id: this.agent_id
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.data = res.data
					if (res.data.myPolicy && res.data.myPolicy.trades && res.data.myPolicy != null) {
						this.myTradesForm = res.data.myPolicy.trades
						this.myActivesForm = res.data.myPolicy.actives
						this.mySimcardsForm = res.data.myPolicy.simcards
						this.myStandardsForm = res.data.myPolicy.standards
					}
					if (res.data.agentPolicy && res.data.agentPolicy != null && res.data.agentPolicy.trades) {
						this.toTradesForm = res.data.agentPolicy.trades
						this.toActivesForm = res.data.agentPolicy.actives
						this.toSimcardsForm = res.data.agentPolicy.simcards
						this.toStandardsForm = res.data.agentPolicy.standards
						this.agentPolicy = res.data.agentPolicy
					}
					this.settings = res.data.settings
				}
			},
			// 保存数据
			saveData() {
				var _this = this
				// 交易分润 - 验证
				for (var i in this.toTradesForm) {
					if (this.toTradesForm[i].money == '') {
						uni.showToast({
							title: '请设置分润 - 结算费率！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
					if (this.toTradesForm[i].money * 100 < 0 || this.toTradesForm[i].money * 100 > this.toTradesForm[i].max_money * 100) {
						uni.showToast({
							title: '分润设置超出限制！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
				}
				// 激活 - 验证
				for (var i in this.toActivesForm) {
					if (this.toActivesForm[i].money == '') {
						uni.showToast({
							title: '请设置激活 - 返现金额！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
					if (this.toActivesForm[i].money * 100 < 0 || this.toActivesForm[i].money * 100 > this.toActivesForm[i].max_money * 100) {
						uni.showToast({
							title: '激活设置超出限制！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
				}
				// 流量卡 - 验证
				for (var i in this.toSimcardsForm) {
					if (this.toSimcardsForm[i].money == '') {
						uni.showToast({
							title: '请设置流量卡 - 返现金额！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
					if (this.toSimcardsForm[i].money * 100 < 0 || this.toSimcardsForm[i].money * 100 > this.toSimcardsForm[i].max_money * 100) {
						uni.showToast({
							title: '流量卡设置超出限制！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
				}
				// 达标 - 验证
				for (var i in this.toStandardsForm) {
					if (this.toStandardsForm[i].money == '') {
						uni.showToast({
							title: '请设置达标 - 奖励金额！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
					if (this.toStandardsForm[i].money * 100 < 0 || this.toStandardsForm[i].money * 100 > this.toStandardsForm[i].max_money * 100) {
						uni.showToast({
							title: '达标设置超出限制！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
				}
				uni.showModal({
					content: this.policyMsg,
					success: (res) => {
						if (res.confirm) {
							_this.saveDataFun()
						}
					}
				})
			},
			// 保存数据 - 方法
			async saveDataFun() {
				if (this.is_true) {
					this.is_true = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp({
						url: 'v1/saveAgentHzyPolicy',
						data: {
							trades: this.toTradesForm,
							actives: this.toActivesForm,
							standards: this.toStandardsForm,
							simcards: this.toSimcardsForm,
							settings: this.settings,
							agent_id: this.agent_id,
							time_type: this.agentPolicy.time_type,
							trade_type: this.agentPolicy.trade_type,
							id: this.agentPolicy.id
						}
					});
					uni.hideLoading()
					this.is_true = true
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					})
					if (res.code == 200) {
						setTimeout(function() {
							uni.navigateBack({});
						}, 1000)
					}
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F2F6FC;
	}

	input {
		font-size: 28rpx;
		padding: 10rpx;
		border-radius: 10rpx;
		width: 200rpx;
		background-color: #DCDFE6;
	}

	.main {
		.mains {

			.top_c {
				padding: 30rpx;
				background-color: #F2F6FC;

				.top {
					padding: 0 30rpx;
					background-color: #ffffff;
					border-radius: 18rpx;
					box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);

					.items {
						padding: 30rpx 0;
						display: flex;
						justify-content: space-between;
						border-bottom: 1px solid #EBEEF5;

						.lefts {}

						.rights {
							display: flex;
							align-items: center;

							.phone {
								padding-left: 16rpx;
							}

							.products {
								padding-right: 16rpx;
							}
						}
					}
				}

				.tags {
					margin-top: 30rpx;
				}
			}

			.content {
				padding: 0 30rpx;

				.types {
					padding-bottom: 90rpx;

					.lists {
						padding: 30rpx 30rpx 0 30rpx;
						background-color: #ffffff;
						border-radius: 18rpx;
						box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
						margin-bottom: 30rpx;

						.title {
							text-align: center;
							padding-bottom: 30rpx;
							border-bottom: 1px solid #EBEEF5;
							font-weight: bold;
							display: flex;
							justify-content: space-between;
							align-items: center;

							.rights {
								display: flex;
								align-items: center;

								.setZore {
									padding: 4rpx 18rpx;
									font-size: 24rpx;
									background-color: #fa3534;
									border-radius: 8rpx;
									color: #ffffff;
									font-weight: normal;
									margin-right: 16rpx;
								}
							}
						}

						.items {
							display: flex;
							justify-content: space-between;
							align-items: center;
							padding: 20rpx 0;
							border-top: 1px solid #EBEEF5;

							.rights {
								display: flex;
								justify-content: flex-end;
								align-items: center;

								input {
									padding-right: 60rpx;
								}

								text {
									position: absolute;
									padding-right: 10rpx;
								}

								.flow_unit {
									padding-right: 80rpx;
								}
							}
						}

						.items-add {
							border-top: 1px solid #EBEEF5;
							padding: 20rpx 0rpx;
							color: #2979ff;

							.add-btn {
								display: flex;
								justify-content: center;
								align-items: center;
								text-align: center;

								text {
									padding-left: 10rpx;
								}
							}
						}
						
						.list-item {
							margin-top: 30rpx;
							border-radius: 10rpx;
							border: 1px dashed #c8c9cc;
							padding: 20rpx 20rpx 0 20rpx;
							
							.dayFrame {
								padding: 20rpx 0;
								display: flex;
								border-bottom: 1px solid #EBEEF5;
								align-items: center;
								text-align: center;
							
								.lefts {
									width: 40%;
								}
							
								.centers {
									width: 20%;
								}
							
								.rights {
									width: 40%;
									display: flex;
									justify-content: flex-end;
								}
							}
						}
						
						.list-item:last-child {
							margin-bottom: 30rpx;
						}
					}
					
					.standard {
						padding-bottom: 10rpx;
					}

					.lists-add {
						padding: 30rpx 0rpx;
						background-color: #2979ff;
						color: #ffffff;
						border-radius: 18rpx;
						box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);

						.add-btn {
							display: flex;
							justify-content: center;
							align-items: center;
							text-align: center;

							text {
								padding-left: 10rpx;
							}
						}
					}
				}
			}

			.saveBtn {
				position: fixed;
				left: 0;
				right: 0;
				bottom: 0;
				text-align: center;
				background-color: #409EFF;
				color: #ffffff;
				padding: 20rpx 0;
			}
		}
	}
</style>